package com.sff.site.tree {
	import com.sff.core.sff_internal;
	import com.sff.site.events.NodeEvent;
	import com.sff.site.node.Node;
	import com.sff.site.abstract.AbstractNode;
	import com.sff.site.abstract.AbstractPath;
	import com.sff.site.core.INode;
	import com.sff.site.core.IPath;
	import com.sff.site.node.NodeDescriptor;
	
	import flash.net.URLVariables;	
	
	public class TreeNode extends AbstractNode {
		
		public function TreeNode( parent : TreeNode ) {
			super( parent );
		}

		public override function isActive() : Boolean {
			return _active;
		}
		
		public override function activate( params : URLVariables = null ) : void {
			( path( ) as SiteTreePath ).activate( params );
		}
		
		override public function setParams(params : URLVariables) : void {
			super.setParams( params );
			dispatchEvent( new NodeEvent( NodeEvent.PARAM_CHANGE , this ) );
		}
		
		public override function describe(descriptor : NodeDescriptor) : void {
			if( descriptor is TreeDescriptor ) return;
			_id = descriptor.id;
			if( descriptor.defaultId )
				sff_internal::setDefaultId( descriptor.defaultId );			
		}

		override public function path( getDefaults : Boolean = false ) : IPath {
			var segs : Array = new Array();
			var node : INode = this;
			if( getDefaults && _default ) {
				segs.push( _default );
			}
			do {
				segs.push( node.getId() );
			} while ( node = node.parent() );
			segs.reverse();
			var device : String = segs.shift( );
			var pathStr : String = segs.join( AbstractPath.SEPARATOR );
			return new SiteTreePath( pathStr , device );
		}

		override public function getCurrentChild() : INode {
			return null;
		}

		override protected function _dispose() : void {
			throw new Error( "TreeNode - dispose : dangerous methods call" );
		}

		sff_internal function createNode( parent : Node ) : Node {
			var n : Node = new Node( parent, this );
			return n;
		}
		
		internal function setActive( flag : Boolean ) : void {
			_active = flag;
		}

		protected var _active : Boolean;		
	}
}
